﻿using System;
using System.Windows.Forms;
using DosTexPrj.Core.Models;
using DosTexPrj.Core.RepositoriesInterface;
using DosTexPrj.Implement.NHibernate;
using DosTexPrj.UI.WinForm.Commons;

namespace DosTexPrj.UI.WinForm
{
    public partial class DyeOrderDetailForm : Form
    {
        #region --- Private members ---

        private IRepository _repository;

        #endregion --- Private members ---

        #region --- Properties ---

        public DyeOrderDetail CurrentOrderDetail
        {
            get;
            private set;
        }

        #endregion --- Properties ---

        #region --- Constructors ---

        public DyeOrderDetailForm()
        {
            InitializeComponent();
            _repository = MyServiceLocator.DoGetInstance<IRepository>();
            this.CurrentOrderDetail = null;
            this.txtWeight.KeyPress += new KeyPressEventHandler(Textbox_KeyPress);
            this.txtLength.KeyPress += new KeyPressEventHandler(Textbox_KeyPress);
            this.txtWeight.MaxLength = this.txtLength.MaxLength = 12;
        }

        private DyeOrderDetailForm(DyeOrderDetail orderDetailToEdit)
            : this()
        {
            this.CurrentOrderDetail = orderDetailToEdit;
        }

        #endregion --- Constructors ---

        private void KnittingOrderDetailForm_Load(object sender, EventArgs e)
        {
            // Clear form
            ClearForm();
        }

        private void Textbox_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.HandlingNumberTextbox(e);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValidate())
            {
                this.CurrentOrderDetail = null;
                return;
            }
            this.CurrentOrderDetail = new DyeOrderDetail();
            this.CurrentOrderDetail.IsCompact = chkCompact.Checked;
            this.CurrentOrderDetail.IsCuttingGomming = chkCuttingGomming.Checked;
            this.CurrentOrderDetail.IsEnzim = chkEnzim.Checked;
            this.CurrentOrderDetail.IsResine = chkResine.Checked;
            this.CurrentOrderDetail.IsRolling = chkRolling.Checked;
            this.CurrentOrderDetail.IsSilicon = chkSilicon.Checked;

            double length = 0;
            double.TryParse(txtLength.Text.Trim(), out length);
            this.CurrentOrderDetail.Length = length;

            this.CurrentOrderDetail.SpecialFinishing = txtSpecialFinishing.Text.Trim();

            double weight = 0;
            double.TryParse(txtWeight.Text.Trim(), out weight);
            this.CurrentOrderDetail.Weight = weight;

            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        #region Functions

        private void ClearForm()
        {
            // Clear textboxes
            txtLength.Text = string.Empty;
            txtWeight.Text = string.Empty;
            txtSpecialFinishing.Text = string.Empty;
            // Check boxes
            chkCompact.Checked = chkCuttingGomming.Checked = chkEnzim.Checked = chkResine.Checked = chkRolling.Checked = chkSilicon.Checked = false;
        }

        private bool CheckValidate()
        {
            error.Clear();
            bool flag = true;
            if (!Common.IsBlankTextBox(txtLength, "Required field!", error))
                flag = false;
            if (!Common.IsBlankTextBox(txtWeight, "Required field!", error))
                flag = false;
            return flag;
        }

        #endregion Functions
    }
}